package com.blstudio.ghgallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blstudio.R;
import com.blstudio.ghgallery.common.GHGallery;
import com.blstudio.ghgallery.common.GHGalleryConstant;
import com.blstudio.ghgallery.common.ImageDownloader;


public class GHGalleryDetail extends Activity {

	private GHGallery mGallery;

	private final ImageDownloader imageDownloader = new ImageDownloader();

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGallery.onGalleryTouchEvent(event);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		int position = intent.getIntExtra("position", 0);

		mGallery = new GHGallery(this, R.id.iv);
		mGallery.setPaddingWidth(5);
		
		mGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,
			GHGalleryConstant.getInstance().getGalleryConstant())
		{
			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{

				if (convertView == null) {
					LayoutInflater inflater = getLayoutInflater();
					convertView = inflater.inflate(R.layout.image_detail, parent, false);
				}

				ImageView iv = (ImageView)convertView.findViewById(R.id.iv);

				imageDownloader.download(GHGalleryConstant.getInstance().getGalleryConstant()[position], iv);

				return convertView;
			}
		}, position);

		LinearLayout layout = new LinearLayout(getApplicationContext());
		layout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);

		layoutParams.setMargins(10, 10, 10, 10);
		layoutParams.weight = 1.0f;

		layout.addView(mGallery, layoutParams);

		setContentView(layout);
	}

}